package com.supcon.exchangedata.utils;

import com.supcon.exchangedata.config.CodeMsg;
import com.supcon.exchangedata.enums.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * @author ：icerain.gan
 * @date ：Created in 2020/3/11 23:28
 * @description：全局异常处理类
 * @modified By：
 * @version: 1.0$
 */
@ControllerAdvice
@Slf4j
@EnableConfigurationProperties(value = CodeMsg.class)
public class ControllerExceptionHandler {

    @Autowired
    private CodeMsg codeMsg;

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Response handleException(Exception e) {
        e.printStackTrace();
        if (e.getMessage().matches("\\d+")) {
            // 若返回的是数字这配置文件取值
            return new Response(1, ResultEnum.FAILED_LOGIC.setResultEnum(e.getMessage(), codeMsg.getMsg(e.getMessage())), null);
        } else if (e.getMessage().contains("Exception")) {
            // 系统抛出异常 显示操作失败
            return new Response(1, ResultEnum.FAILED_SYSTEM, null);
        } else {
            // validate 验证异常
            return new Response(1, ResultEnum.FAILED_VALIDATE.appendMsg(e.getMessage()), null);
        }
    }

//    @ExceptionHandler(InspectionException.class)
//    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
//    public Response handleInspectionException(InspectionException e) {
//        return new Response(1, ResultCode.FAILED_INSPECTION.appendMsg(e.getMessage()), null);
//    }

    /**
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Response handleConstraintViolationException(ConstraintViolationException e) {
        log.error(e.getMessage());

        Set<ConstraintViolation<?>> set = e.getConstraintViolations();
        StringBuilder msg = new StringBuilder();
        if (set != null && set.size() > 0) {
            for (ConstraintViolation<?> constraintViolation : set) {
                msg.append(constraintViolation.getMessage());
                msg.append(",");
            }
        }
        return new Response(1, ResultEnum.FAILED_VALIDATION.appendMsg(msg.toString()), null);
    }


    /**
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Response handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder errorMesssage = new StringBuilder();

        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMesssage.append(fieldError.getDefaultMessage());
            errorMesssage.append(",");
        }
        return new Response(1, ResultEnum.FAILED_VALIDATION.appendMsg(errorMesssage.toString()),null);
    }
}
